﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace Voize
{
    internal partial class fId3 : Form
    {


        internal fId3(List<string> Playlist)
        {
            InitializeComponent();
            m_FileCollection = new List<string>();
            string artist = null;
            string song = null;
            CTagger tagger = new CTagger();

            this.Cursor = Cursors.WaitCursor;

            foreach (string file in Playlist)
            {
                //Support for ogg and mp3 only
                if (file.EndsWith(".mp3") || file.EndsWith(".ogg"))
                {
                    m_FileCollection.Add(file);
                    artist = tagger.GetFirstArtist(file);
                    song = tagger.GetSong(file);
                    
                    if (artist != null && song != null)
                    {
                        this.lPlaylist.Items.Add(artist.Replace("-", "") + " - " + song).ForeColor = Color.DarkSeaGreen;
                    }
                    else
                    {
                        this.lPlaylist.Items.Add(Path.GetFileNameWithoutExtension(file));
                    }
                }
            }

            this.Cursor = Cursors.Default;

            m_Tagger = new CTagger();
        }



        private CTagger m_Tagger;
        private List<string> m_FileCollection;


        private void Save()
        {
            try
            {
                if (this.tFilename.Text != "")
                    m_Tagger.SetBasicTags(this.tArtist.Text, this.tSong.Text, this.tFilename.Name);
            }
            catch (IOException e)
            {
                //Cannot change playing song name.
                fMessagebox box = 
                    new fMessagebox("File is in use.", 
                                    "Cannot change tags when the file is playing. Try another one or stop the player. Exact exception: " + 
                                    e.Message);
                box.ShowDialog();
            }
        }



        private void lPlaylist_ItemActivate(object sender, EventArgs e)
        {
            string file = m_FileCollection[this.lPlaylist.SelectedItems[0].Index];
            
            this.tFilename.Name = file;
            this.tFilename.Text = Path.GetFileNameWithoutExtension(file);
            this.tArtist.Text = m_Tagger.GetFirstArtist(file);
            this.tSong.Text = m_Tagger.GetSong(file);

            this.bOK.Enabled = true;
            this.bApply.Enabled = true;
        }



        private void bApply_Click(object sender, EventArgs e)
        {
            this.Save();
            //Mark with different color.
            this.lPlaylist.SelectedItems[0].ForeColor = Color.Green;
        }



        private void bOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Save();
            this.Close();
        }



        private void bCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }


    }
}
